package com.lihan.GetSeatInLib.Controllers;

import java.util.Date;
import java.util.HashMap;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMailMessage;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.lihan.GetSeatInLib.Domain.StandReturnMessage;
import com.lihan.GetSeatInLib.Domain.User;
import com.lihan.GetSeatInLib.Domain.MailModel.MailTemplate;
import com.lihan.GetSeatInLib.Service.JwtService;
import com.lihan.GetSeatInLib.Service.MailService;
import com.lihan.GetSeatInLib.Service.UserService;
import com.lihan.GetSeatInLib.Tools.BeanUtils;
import com.lihan.GetSeatInLib.Tools.MyEncoder;
import com.lihan.GetSeatInLib.Tools.PassTokenAnnoation;

import io.netty.util.internal.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;


@Api(tags = "用户操作Controller")
@RestController
@RequestMapping("/User")
public class UserController {

	@Autowired
	private UserService userService;
	
	@Autowired
	private JwtService jwtService;   //jwt签发
	
	@Autowired
	private MyEncoder encoder; //密码加密
	
	@Autowired
	@Qualifier("redisTemplate")
	private RedisTemplate<String, String> redisTemplate;
	
	//根据ID获取用户信息
	@PassTokenAnnoation
	@RequestMapping(value = "/GetInfo/{ID}",method = RequestMethod.GET)
	@ApiOperation("根据ID获取用户信息")
	public StandReturnMessage<User> getUserByID(@PathVariable int ID) {
		StandReturnMessage<User> res = new StandReturnMessage<>();
		res.setCode(200);
		res.setData(userService.getUserInfoByID(Long.valueOf(ID)));
		res.setStatus(1);
		return res;
	}
	
	@PassTokenAnnoation
	@ApiOperation("根据用户名查询用户")
	@GetMapping("/GetInfoByName")
	public StandReturnMessage<User> getUserByName(String UserName){
		User user = userService.getUserInfoByUserName(UserName);
		if(user!=null)
			return new StandReturnMessage<User>(200, user, 1);
		else
			return new StandReturnMessage<User>(400, null, 0);
	}
	
	//注册新用户
	@PassTokenAnnoation
	@RequestMapping(value = "/RegisterNew",method = RequestMethod.POST)
	@ApiOperation("注册新的用户")
	public StandReturnMessage<String> insertNewUser(String UserName,
													String Password,
													String Repassword,
													String PhoneNumber,
													String Email,
													String StudentID,
													String sex
													) throws Exception{
		
		if(StringUtil.isNullOrEmpty(UserName)||
				StringUtil.isNullOrEmpty(Password)||
				StringUtil.isNullOrEmpty(Repassword)||
				StringUtil.isNullOrEmpty(PhoneNumber)||
				StringUtil.isNullOrEmpty(Email)||
				StringUtil.isNullOrEmpty(StudentID)||
				StringUtil.isNullOrEmpty(sex))
			throw new Exception("以上信息为必填");
		if(!Password.equals(Repassword))
			return new StandReturnMessage<>(400,"两次输入不一致",0);
		
		StandReturnMessage<String> res = new StandReturnMessage<String>();
		User user = BeanUtils.BlankUser(UserName, encoder.Encoder(Password), PhoneNumber, Email, StudentID, sex);
		userService.InsertNewUser(user);
		
		
		res.setCode(200);
		res.setData("插入成功");
		res.setStatus(1);
		return res;
	}
	
	//用户登录方法，在返回中会带JWT
	@PassTokenAnnoation
	@ApiOperation("登录方法")
	@PostMapping("/Login")
	public StandReturnMessage<Boolean> LoginPassword(String UserName,String Password){
		StandReturnMessage<Boolean> res = new StandReturnMessage<Boolean>();
		
		User user = userService.LoginWirhPassword(UserName, encoder.Encoder(Password));
		System.out.println(encoder.Encoder(Password));
		if(user!=null) {
			res.setCode(200);
			res.setData(true);
			res.setStatus(1);
			//生成Token
			res.setAttch(jwtService.createTokenByUserName(user));
			return res;
		}
		
		res.setCode(400);
		res.setData(false);
		res.setStatus(0);
		//生成Token
		res.setAttch("jwt验证过期或无效");
		return res;
		
	}
	
	//用户修改密码
	/**
	 * 
	 * @Title: ReSetPassword
	 * @Description: 修改密码接口，要完成密码修改的动作，必须先调用发送邮箱验证码的接口
	 * @param @param UserName
	 * @param @param OrginPassword
	 * @param @param NewPassword
	 * @param @param ReNewPassword
	 * @param @return 参数
	 * @return StandReturnMessage<Boolean> 返回类型
	 * @throws MessagingException 
	 * @throws
	 */
	@ApiOperation("用户修改密码接口")
	@RequestMapping(value = "/ResetPassword",method = RequestMethod.POST)
	@PassTokenAnnoation
	public StandReturnMessage<Boolean> ReSetPassword(String UserName,
													 String NewPassword,
													 String ReNewPassword,
													 String TRY_VeriferCode) throws MessagingException{
		
		if(!NewPassword.equals(ReNewPassword)) {
			new StandReturnMessage<Boolean>(400,false,1,"两次输入不一致");
		}
		
		if(userService.FixPassword(UserName, NewPassword, ReNewPassword, TRY_VeriferCode)) {
			return new StandReturnMessage<Boolean>(200,true,1,"成功修改密码");
		}else {
			return new StandReturnMessage<Boolean>(400,false,1,"验证码失效或错误");
		}
			
	}
	
	@ApiOperation("发送修改密码验证码")
	@PostMapping("/SenderFixPWD_Mail")
	@PassTokenAnnoation
	public StandReturnMessage<Boolean> SenderFixPasswordMail(String UserName) throws MessagingException{
	   boolean result = userService.SendFixPasswordMail(UserName);
	   if(result)
		   return new StandReturnMessage<Boolean>(200, result, 1);
	   else
		   return new StandReturnMessage<Boolean>(400, result, 0,"发送验证码失败");
	}
	
	
	//用户登出,把Header中的验证信息拿掉即可
	@RequestMapping(value = "/Logout",method = RequestMethod.GET)
	@ApiOperation("用户登出操作")
	public StandReturnMessage<String> UserLogout(@ApiIgnore HttpServletRequest requset,
												@ApiIgnore HttpServletResponse resp){
		if(requset.getHeader("Authorize")!=null&&requset.getHeader("username")!=null) {
			resp.setHeader("Authorize", null);
			resp.setHeader("username", null);
		}
		return new StandReturnMessage<String>(200,"已登出",1);
	}
	
	

	
}
